//
//  NSString+Tools.m
//  CreditGroup
//
//  Created by JPlay on 14/10/21.
//  Copyright (c) 2014年 JPlay. All rights reserved.
//

#import "NSString+Tools.h"

@implementation NSString (Tools)

-(NSMutableAttributedString *)attributedStringFromStingWithFont:(UIFont *)font
                                                withLineSpacing:(CGFloat)lineSpacing
{
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:self attributes:@{NSFontAttributeName:font}];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpacing];
    
    [attributedStr addAttribute:NSParagraphStyleAttributeName
                          value:paragraphStyle
                          range:NSMakeRange(0, [self length])];
    return attributedStr;
}

-(CGSize)boundingRectWithSize:(CGSize)size
                 withTextFont:(UIFont *)font
              withLineSpacing:(CGFloat)lineSpacing
{
    NSMutableAttributedString *attributedText = [self attributedStringFromStingWithFont:font
                                                                        withLineSpacing:lineSpacing];
    CGSize textSize = [attributedText boundingRectWithSize:size
                                                   options:NSStringDrawingUsesLineFragmentOrigin
                                                   context:nil].size;
    return textSize;
}

+ (NSMutableDictionary*)handleUrl:(NSString*)urlStr
{
    NSMutableDictionary *paraDic = nil;
    NSUInteger quesMarkLocation = [urlStr rangeOfString:@"?"].location;
    if (quesMarkLocation != NSNotFound) {
        
        NSString *AfQuesStr = [urlStr substringFromIndex:quesMarkLocation];
        NSArray *paraArr = [AfQuesStr componentsSeparatedByString:@"&"];
        for (NSString *string in paraArr) {
            
            NSArray *keyValuePara = [string componentsSeparatedByString:@"="];
            [paraDic setObject:keyValuePara[1] forKey:keyValuePara[0]];
        }
    }
    
    return paraDic;
}

@end
